<?php

namespace App\Admin\Controllers\System;

use App\Models\Common\Area as SystemRegion;
use App\Http\Controllers\Controller;
use App\Models\Common\CommonParam;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Illuminate\Support\Facades\Storage;
class RegionController extends AdminController
{

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->title($this->title())
            ->description($this->description['index'] ?? trans('admin.list'))
            ->body(SystemRegion::tree());

//        return Admin::content(function (Content $content) {
//            $content->header('树状模型');
//            $content->body(SystemRegion::tree(function ($tree) {
//                $tree->branch(function ($branch) {
//                    return "{$branch['text']}  code [{$branch['code']}]    pcode 【 {$branch['pcode']} 】 "; #id:{$branch['id']}
//                });
//            }));
//        });

//        return $content
//            ->header('Index')
//            ->description('description')
//            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new SystemRegion);

        $grid->id('Id');
        $grid->code('code');
        $grid->pcode('pcode');
        $grid->text('Text');
//        $grid->create_time('Create time');
//        $grid->update_time('Update time');

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(SystemRegion::findOrFail($id));

        $show->id('Id');
        $show->pid('Pid');
        $show->text('Text');
        $show->create_time('Create time');
        $show->update_time('Update time');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new SystemRegion);

        $form->text('pcode', 'pcode');
        $form->text('text', 'Text');
        $form->text('code','code');
//        $form->datetime('create_time', 'Create time')->default(date('Y-m-d H:i:s'));
//        $form->datetime('update_time', 'Update time')->default(date('Y-m-d H:i:s'));

        return $form;
    }

    public function refresh(Content $content){
        $data = SystemRegion::select('code','pcode','text')
            ->orderBy('sort','asc')
            ->get()->toArray();
        $res = list_tree_Intelligence($data,'code','pcode','children');
        $json = json_encode($res);
        $disk = Storage::disk('public');
        $disk->put('area.json',$json);
//         dd(123);
        return $content
            ->header('刷新地址数据包')
            ->description('刷新成功'.'   /area.json')
            ->body($this->grid());
    }
}
